using System;
using System.Data;
using System.Data.SqlClient;
using DataAccessLayer;
using EntityObjectLayer;

namespace BusinessLogicLayer
{
    public class BLUserLogin
    {
        #region Khai bao bien

        public static bool isUserActive;
        public static String[] param;

        #endregion

        #region Function Validate User Login

        /// <summary>
        /// userLogin : ham thuc hien viec kiem tra nguoi dung dang nhap
        /// v. 1.0.0.0
        /// author: ducpnt
        /// </summary>
        /// <param name="usn">tham so ten tai khoan ngoi dung</param>
        /// <param name="pwd">tham so mat khau cua nguoi dung</param>


        public int validateUserLogin(BEUsers beusers)       

        {
            SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn.Value = beusers.sUserName;
            SqlParameter pwd = new SqlParameter("@Pass", SqlDbType.Char, 30);
            pwd.Value = beusers.sPass;
            SqlParameter usn_v2 = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn_v2.Value = beusers.sUserName;
            SqlDataReader drSession = Common.GetFromDataReader("pr_tblSession_SelectAll", usn);
            if (drSession.HasRows)
            {
                return 2;
            }
            else
            {
                SqlDataReader dr = Common.GetFromDataReader("pr_tblUsers_SelectAll", usn_v2, pwd);

                //doan nay de them user vao bang session
                SqlParameter usn_v3 = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
                usn_v3.Value = beusers.sUserName;

                try
                {
                    Common.Execute("pr_tblSession_Insert", usn_v3);
                }
                catch (Exception e)
                {
                    throw;
                }
                //
                return (dr.HasRows) ? 1 : 0;
            }
        }

        #endregion

        #region Function User Logout

        public int userLogout(BEUsers beu) 
        {
            SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn.Value = beu.sUserName;
            try
            {
                Common.Execute("pr_tblSession_Update", usn);
            }
            catch (Exception e)
            {
                throw;
            }

            return 1;
        }

        #endregion
    }
}